package com.dashu.device.service;



import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;

@Service
public class DataService {


    @Resource
    public RestTemplate restTemplate;

    @Autowired
    @PersistenceContext
    public
    EntityManager entityManager;

    @Autowired
    @PersistenceContext(unitName = "tenantPersistenceUnit")//注意引号的内容与配置文件一致
    EntityManager entityManager1;


    public JPAQueryFactory queryFactory;

    JPAQueryFactory queryFactory1;

    @PostConstruct
    public void initFactory() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    @PostConstruct
    public void initFactory1() {
        queryFactory1 = new JPAQueryFactory(entityManager1);
    }


    @SuppressWarnings("unchecked")
    public <T> T findObject(String hql, Object param) throws Exception {
        List<Object> dataList = findHql(hql, Collections.singletonList(param));
        if (dataList != null && dataList.size() > 0) {
            return (T) dataList.iterator().next();
        }
        return null;
    }


    @SuppressWarnings("unchecked")
    public <T> List<T> findHql(String hql, List<Object> params, int startIndex, int pageSize) {
        Query query = entityManager.createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                query.setParameter(i + 1, params.get(i));
            }
        }

        if (startIndex >= 0 && pageSize > 0) {
            query.setFirstResult(startIndex);
            query.setMaxResults(pageSize);
        }
        return query.getResultList();
    }


    @SuppressWarnings("unchecked")
    public <T> List<T> findHql(String hql, List<Object> params) {
        Query query = entityManager.createQuery(hql);
        if (params != null && !params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                query.setParameter(i + 1, params.get(i));
            }
        }
        return query.getResultList();
    }


    @SuppressWarnings("unchecked")
    public <T> List<T> findSql(String sql, List<Object> params, int startIndex, int pageSize) {
        Query query = entityManager.createNativeQuery(sql);
        if (params != null && !params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                query.setParameter(i + 1, params.get(i));
            }
        }
        if (startIndex >= 0 && pageSize > 0) {
            query.setFirstResult(startIndex);
            query.setMaxResults(pageSize);
        }
        return query.getResultList();
    }


    @SuppressWarnings("unchecked")
    public <T> List<T> findSql(String sql, List<Object> params) {
        Query query = entityManager.createNativeQuery(sql);
        if (params != null && !params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                query.setParameter(i + 1, params.get(i));
            }
        }
        return query.getResultList();
    }


}
